package com.heima.wemedia.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.CustomException;
import com.heima.file.service.FileStorageService;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import com.heima.wemedia.utils.WmThreadLocalUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Resource
    private FileStorageService fileStorageService;

    @Override
    @Transactional
    public ResponseResult<WmMaterial> uploadPicture(MultipartFile multipartFile) {
        try {
            //上传文件到minio中
            String postFix = StrUtil.subAfter(multipartFile.getOriginalFilename(), '.', true);
            String fileName = StrUtil.format("{}.{}", IdUtil.simpleUUID(), postFix);
            String imageUrl = this.fileStorageService.uploadImgFile(null, fileName, multipartFile.getInputStream());

            //保存到数据库
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUrl(imageUrl);
            wmMaterial.setType(Convert.toShort(0));
            wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
            wmMaterial.setIsCollection(Convert.toShort(0));
            wmMaterial.setCreatedTime(new Date());
            super.save(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        } catch (Exception e) {
            throw new CustomException(AppHttpCodeEnum.FILE_UPLOAD_ERROR);
        }
    }

    /**
     * 素材列表查询
     */
    @Override
    public PageResponseResult<List<WmMaterial>> findList(WmMaterialDto dto) {
        //1.检查参数
        dto.checkParam();
        //2.分页查询
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = Wrappers.lambdaQuery();
        //是否收藏
        lambdaQueryWrapper.eq(ObjectUtil.equal(dto.getIsCollection(), 1), WmMaterial::getIsCollection, dto.getIsCollection());

        //按照用户查询
        lambdaQueryWrapper.eq(WmMaterial::getUserId, WmThreadLocalUtil.getUser().getId());

        //按照时间倒序
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //查询数据
        super.page(page, lambdaQueryWrapper);

        //3.结果返回
        return PageResponseResult.<List<WmMaterial>>builder()
                .currentPage(dto.getPage())
                .size(dto.getSize())
                .total(Convert.toInt(page.getTotal()))
                .data(page.getRecords())
                .build();
    }
}
